package cn.lzd.mr.count.combinetext;

import cn.lzd.mr.count.WordCountM;
import cn.lzd.mr.count.WordCountR;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 学习：先了解、后深入；先记录、后记忆；先理论、后实践；先模仿、后创新
 * Created by lzd on 2018/5/12.
 */
public class WordCountMR {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //创建job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //指定jar加载路径
        job.setJarByClass(WordCountMR.class);

        //设置map和reduce类
        job.setMapperClass(WordCountM.class);
        job.setReducerClass(WordCountR.class);
        //设置map输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设置reduce输出
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        /**
         * 默认的话每个文件至少会才生一个maptask,這样的话如果是多个小文件很浪费资源。
         * 可以做如下优化
         */
        job.setInputFormatClass(CombineTextInputFormat.class);
        //一个maptask最少读2m
        CombineTextInputFormat.setMinInputSplitSize(job,2097152);
        CombineTextInputFormat.setMaxInputSplitSize(job,4194304);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //提交
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);
    }
}
